package cn.springspace.kotlinapp.service

import cn.springspace.kotlinapp.model.DoorEvent
import cn.springspace.kotlinapp.repository.DoorEventRepository
import org.springframework.stereotype.Component
import org.springframework.web.reactive.function.BodyInserters.fromObject
import org.springframework.web.reactive.function.server.ServerRequest
import org.springframework.web.reactive.function.server.ServerResponse
import reactor.core.publisher.Mono

@Component
class RestDoorEventHandler(val doorEventRepository: DoorEventRepository) {
    fun saveDoorEvent(request: ServerRequest): Mono<ServerResponse> = request.bodyToMono(DoorEvent::class.java)
            .flatMap { it -> Mono.just(doorEventRepository.save(it)) }
            .flatMap { it ->
                ServerResponse.ok().body(fromObject(it))
            }
}